﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace MendeleyClient.Phone
{
    public partial class StatPage : PhoneApplicationPage
    {
        string current_id = "0";

        // Constructor
        public StatPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            this.LayoutAuthor.DataContext = App.AuthorViewModel;
            this.LayoutPaper.DataContext = App.DocumentViewModel;
            this.LayoutPublication.DataContext = App.PublicationViewModel;
            this.CategoryPicker.DataContext = App.CategoryViewModel;

            this.Loaded += new RoutedEventHandler(StatPage_Loaded);
        }

        // Load data for the ViewModel Items
        private void StatPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.TitleBarTextBlock.Text = "Top Stats";
            if (!App.CategoryViewModel.IsDataLoaded)
            {
                App.CategoryViewModel.LoadData();
            }
            RefreshData();
        }

        private void RefreshData(bool force = false)
        {
            this.RefreshProgressBar.Visibility = System.Windows.Visibility.Visible;
            if (force)
            {
                App.AuthorViewModel.Items.Clear();
                App.AuthorViewModel.IsDataLoaded = false;
                App.DocumentViewModel.Items.Clear();
                App.DocumentViewModel.IsDataLoaded = false;
                App.PublicationViewModel.Items.Clear();
                App.PublicationViewModel.IsDataLoaded = false;
            }

            if (!App.AuthorViewModel.IsDataLoaded)
            {
                App.AuthorViewModel.LoadData(current_id);
            }
            if (!App.DocumentViewModel.IsDataLoaded)
            {
                App.DocumentViewModel.LoadData(current_id);
            }
            if (!App.PublicationViewModel.IsDataLoaded)
            {
                App.PublicationViewModel.LoadData();
            }
            this.RefreshProgressBar.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void CategoryPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (App.CategoryViewModel.IsDataLoaded)
            {
                CategoryItemViewModel item = (CategoryItemViewModel)CategoryPicker.SelectedItem;
                current_id = item.Id;
                RefreshData(true);
            }
        }

        private void RefreshAppBarIcon_Click(object sender, System.EventArgs e)
        {
        	RefreshData(true);
        }
		
        private void MendeleyIcon_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void AuthorListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/DocumentPage.xaml", UriKind.Relative));
        }
    }
}